import random
import datetime

import pygame

from lib.sittings import COLORKEY_COLOR, TILE_SIZE, FOOD_COLOR, FOOD_RADIUS, TILE_RECT, SCREENTILES, info


class Food(pygame.sprite.Sprite):
    def __init__(self, takenupgroup):
        pygame.sprite.Sprite.__init__(self)
        self.image = self.image = pygame.Surface(TILE_SIZE).convert()
        self.image.fill(COLORKEY_COLOR)
        self.image.set_colorkey(COLORKEY_COLOR)
        pygame.draw.circle(self.image, FOOD_COLOR, TILE_RECT.center, FOOD_RADIUS)

        self.rect = self.image.get_rect()
        while True:
            self.rect.topleft = (
                random.randint(0, SCREENTILES[0]) * TILE_SIZE[0], random.randint(0, SCREENTILES[1]) * TILE_SIZE[1])
            for sprt in takenupgroup:
                if self.rect.colliderect(sprt):
                    continue  # collision, food cant go here
            break  # no collision, food can go here

def makefood(num, takenupgroup, foodgroup, food_all):
    info(f"[{datetime.datetime.now()}] add food /*{num}*/")
    for i in range(num):
        currentfood = Food(takenupgroup)
        foodgroup.add(currentfood)
        takenupgroup.add(currentfood)
        food_all.add(currentfood)
    return currentfood